<?php
namespace Modules\Admin\Model\Dao\Api;


class Ldap {

    const IS_RETURN = 1;
    const IS_NOT_RETURN = 0;

    const CHECK_TYPE_MENU = '01';
    const CHECK_TYPE_BUTTON = '02';

    /**
     * 票据校验(登录验证)
     * @param $ticket
     * @param int $is_return
     * @return mixed
     * @throws \Base\Exception\Service
     * @throws \S\Exception
     */
    public function validate($ticket, $is_return = self::IS_NOT_RETURN){
        $data = array(
            'ticket'  => $ticket,
            'isReturn'=> $is_return,
        );
        $util = new Util();
        $ret = $util->request("service/validate/ticket",$data);
        if ($ret === false) {
            $code    = $util->getCode();
            if ($code == '9000'){
                $error_config = \S\Config::confError('admin.sign_in_again');
                $msg = sprintf($error_config['msg'], APP_ADMIN_PATH);
                throw new \Base\Exception\Service($msg, $error_config['retcode']);
            }else{
                $message = $util->getMessage();
                throw new \Base\Exception\Service($message, $code);
            }
        } else {
            return $ret;
        }
    }

    /**
     * 票据校验(权限验证)
     * @param $ticket
     * @param string $check_type
     * @param int $is_return
     * @param null $menu_id
     * @param null $button_id
     * @return bool
     * @throws \S\Exception
     */
    public function checkPermission($ticket, $check_type = self::CHECK_TYPE_MENU, $is_return = self::IS_NOT_RETURN, $menu_id = null, $button_id = null){
        $data = array(
            'ticket'  => $ticket,
            'isReturn'=> $is_return,
            'menuId'  => $menu_id,
            'buttonId'=> $button_id,
            'checkType'=> $check_type
        );
        $util = new Util();
        $ret = $util->request("service/validate/checkPermission",$data);
        return $ret;

    }

    /**
     * 登出
     * @param $ticket
     * @return bool
     * @throws \Base\Exception\Service
     * @throws \S\Exception
     */
    public function logout($ticket){
        $data = array(
            'ticket'   => $ticket
        );
        $util = new Util();
        $ret = $util->request("service/logout",$data);
        if ($ret === false) {
            $code    = $util->getCode();
            $message = $util->getMessage();
            throw new \Base\Exception\Service($message, $code);
        } else {
            return true;
        }
    }
}